package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.GuanlichuMapper;
import com.it.tydic.estate.module.mapper.inner.TbCwMapper;
import com.it.tydic.estate.module.mapper.inner.TbCwdaMapper;
import com.it.tydic.estate.module.mapper.inner.TbCwsfMapper;
import com.it.tydic.estate.module.model.Vo.TreeInfo;
import com.it.tydic.estate.module.model.inner.*;
import com.it.tydic.estate.module.service.TbCwdaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @description:   车位档案管理ServiceImpl
 * @author: zhangshuo
 * @createDate: 2020/03/02
 * @version: 1.0
 */
@Service
public class TbCwdaServiceImpl implements TbCwdaService {
    @Autowired
    private TbCwdaMapper tbCwdaMapper;

    @Autowired
    private GuanlichuMapper guanlichuMapper;

    @Autowired
    private TbCwMapper tbCwMapper;
    @Autowired
    private TbCwsfMapper tbCwsfMapper;


    /*车位档案条件查询*/
    @Override
    public Result selectBydaPage(Map<String,Object> tbCwda) {
        Map<String,Object> map = new HashMap<>();
        if(tbCwda.containsKey("pageNum") && tbCwda.containsKey("pageSize")){//判断是否有分页条件有进行分页
            PageHelper.startPage((Integer) tbCwda.get("pageNum"), (Integer) tbCwda.get("pageSize"));
        }
        Page<TbCwda>cheweidanan = tbCwdaMapper.selectBydaPage(tbCwda);
        for (TbCwda tbCwda1 : cheweidanan){
            tbCwda1.setCwdaZt(DictionaryCache.getDicVal(tbCwda1.getCwdaZt()));
        }
        List<TbCwda> result = cheweidanan.getResult();
        map.put("result",result);
        map.put("totle",cheweidanan.getTotal());
        return   Result.success(map);
    }
    /*车位档案批量删除*/
    @Override
    public Result deleteByTbCwda(List<String> list) {
        System.out.print(list);
        if(list==null || list.size()<=0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        int result = tbCwdaMapper.deleteByTbCwda(list);
        if(result<=0){
            return   Result.error(CodeMsg.DELETE_ERROR);
        }
        return Result.success();
    }

    /*车位档案新增或修改*/
    @Override
    public Result insertTbCwdaOrUpdata(TbCwda tbCwda)  {
        long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());//生成Id主键
        if(tbCwda.getcwdaId() == null || "".equals(tbCwda.getcwdaId())) {
            tbCwda.setcwdaId(String.valueOf(id));
            tbCwda.setCwdaZfbj("0");
            tbCwda.setcwdaCjsj(new Date());
            tbCwda.setcwdaXgsj(new Date());
            int result = tbCwdaMapper.insertSelective(tbCwda);
            if (result < 0)
                return Result.error(CodeMsg.INSERT_ERROR);
        }else{
            tbCwda.setcwdaXgsj(new Date());
            int result = tbCwdaMapper.updateByPrimaryKeySelective(tbCwda);
        }
        return Result.success(tbCwda);

    }
    /*根据车位区域查询车位档案*/
    @Override
    public Result selectCwdaByGlc(Map<String,Object> map) {

        List<TbCwda>chewei = tbCwdaMapper.selectCwdaByGlc(map);

        return   Result.success(chewei);
    }


    /*根据管理处查询车位区域*/
    @Override
    public Result selectCwByGlc(Map<String,Object> map) {
        List<TbCwda> quyu=tbCwdaMapper.selectCwByGlc(map);
        return  Result.success(quyu);
    }


    /*根据用户ID获取车位区域左侧导航树*/
    @Override
    public Result findRNAVTreeByUserId(String userId) {
        //获取区域导航,不包含车位
        List<TreeInfo>  treeInfos=findDhByUserIdAndParam(userId,false);
        if(treeInfos.size() > 0){
            return Result.success(treeInfos);
        }
        return Result.success(Collections.EMPTY_LIST);
    }
    /*根据用户ID获取车位收费左侧导航树*/
    @Override
    public Result findCwDhreeByUserId(String userId) {
        //获取车位导航,包含车位
        List<TreeInfo>  treeInfos=findDhByUserIdAndParam(userId,true);
        if(treeInfos.size() > 0){
            return Result.success(treeInfos);
        }
        return Result.success(Collections.EMPTY_LIST);
    }

    public  List<TreeInfo> findDhByUserIdAndParam(String userId,boolean flag){
        //根据用户id获取管理处数据
        List<Guanlichu> guanlichus= getGuanlichuList(userId);
        if(guanlichus.size() > 0){
            //存放管理处的map
            Map<String,Guanlichu> guanlichuMap=new HashMap<>();
            //存放管理处ID
            List<String> glcIds=new ArrayList<String>();
            for(Guanlichu guanlichu : guanlichus){
                glcIds.add(guanlichu.getId());
                guanlichuMap.put(guanlichu.getId(),guanlichu);
            }
            return getGlcMap(guanlichuMap,glcIds,flag);
        }
            return Collections.EMPTY_LIST;
    }





    //根据管理处ids获取所有小区
    public  List<TreeInfo>  getGlcMap(Map<String,Guanlichu> guanlichuMap, List<String> glcIds,boolean flag){
        List<TreeInfo> treeInfos=new ArrayList<>();
        //获取车位管理处的子节点
        Map<String, List<TreeInfo>> cwMap= getTbCwMapByGlcIds(glcIds,flag);
        for(Map.Entry<String,Guanlichu>   glcMap: guanlichuMap.entrySet()){
            TreeInfo treeInfo=new TreeInfo();
            treeInfo.setId(glcMap.getValue().getId());
            treeInfo.setName(glcMap.getValue().getName());
            //根节点parentId为0
            treeInfo.setParentId("0");
            //获取子节点
            if(cwMap.get(glcMap.getKey()) != null){
                treeInfo.setChildren(cwMap.get(glcMap.getKey()));
            }else{
                treeInfo.setChildren(Collections.EMPTY_LIST);
            }
            treeInfos.add(treeInfo);
        }
        return treeInfos;
    }

    /**
     * 获取所有的小区，并且按管理处分组
     * @param glcIds
     * @return
     */
    private Map<String, List<TreeInfo>> getTbCwMapByGlcIds( List<String> glcIds,boolean flag) {
        Map<String,List<TreeInfo>> cwsMap=new HashMap<>();
        Example example = new Example(TbCw.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("cwGlc",glcIds);
        criteria.andEqualTo("cwZfbj",0);
        //获取所有区域
        List<TbCw> tbCws=tbCwMapper.selectByExample(example);
        //获取所有车位
        Map<String,List<TreeInfo>> cwsfMap=new HashMap<>();
        if(flag){
            cwsfMap=getCwSfMap(glcIds);
        }
        //小区按管理处分组
        for(TbCw tbCw : tbCws){
            TreeInfo treeInfo=new TreeInfo();
            treeInfo.setId(tbCw.getcwId());
            treeInfo.setName(tbCw.getCwQymc());
            treeInfo.setParentId(tbCw.getCwGlc());

            //获取区域子节点，车位
            if(cwsfMap.size() > 0){
                treeInfo.setChildren(cwsfMap.get(tbCw.getcwId()));
            }else{
                treeInfo.setChildren(Collections.EMPTY_LIST);
            }
            if(!cwsMap.containsKey(tbCw.getCwGlc())){
                List<TreeInfo> treeInfos=new ArrayList<>();
                treeInfos.add(treeInfo);
                cwsMap.put(tbCw.getCwGlc(),treeInfos);
            }else{
                cwsMap.get(tbCw.getCwGlc()).add(treeInfo);
            }
        }
        return cwsMap;
    }
    /*获取所有车位档案*/
    private Map<String, List<TreeInfo>> getCwSfMap(List<String> glcIds) {
        Map<String,List<TreeInfo>> cwsMap=new HashMap<>();
        Example example = new Example(TbCwda.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("cwdaGlc",glcIds);
        criteria.andEqualTo("cwdaZfbj",0);
        List<TbCwda> cwSfList=tbCwdaMapper.selectByExample(example);
        for(TbCwda tbCwda : cwSfList){
            TreeInfo treeInfo=new TreeInfo();
            treeInfo.setId(tbCwda.getcwdaId());
            treeInfo.setName(tbCwda.getCwdaCwbh());
            treeInfo.setParentId(tbCwda.getCwdaQy());
            treeInfo.setChildren(Collections.EMPTY_LIST);
            if(!cwsMap.containsKey(tbCwda.getCwdaQy())){
                List<TreeInfo> treeInfos=new ArrayList<>();
                treeInfos.add(treeInfo);
                cwsMap.put(tbCwda.getCwdaQy(),treeInfos);
            }else{
                cwsMap.get(tbCwda.getCwdaQy()).add(treeInfo);
            }
        }
        return cwsMap;
    }

    /**
     * 根据用户id获取所有的管理处
     * @param userId
     * @return
     */
    public List<Guanlichu> getGuanlichuList(String userId){
        Example example = new Example(Guanlichu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cuserId",userId);
        criteria.andEqualTo("zfbj",0);
        return guanlichuMapper.selectByExample(example);
    }




}
